<?php 
class Site_setting extends MY_Controller {
	function __construct() {
		parent::__construct();
		$this->load->helper('file');
		//$this->load->model('config_model', '', TRUE);
	}
	
	function site_setting_form() {
		$data[] = array('description' => '站点全称', 'name' => 'SITE_NAME_FULL', 'value' => SITE_NAME_FULL);
		$data[] = array('description' => '站点英文名', 'name' => 'SITE_NAME_ENG', 'value' => SITE_NAME_ENG);
		$data[] = array('description' => '站点中文名', 'name' => 'SITE_NAME_CHS', 'value' => SITE_NAME_CHS);
		$data[] = array('description' => '站点标题名', 'name' => 'SITE_TITLE_CHS', 'value' => SITE_TITLE_CHS);
		$data[] = array('description' => '社团注册说明', 'name' => 'SOCIETY_REGISTER_README', 'value' => str_replace("<br />", "\n", SOCIETY_REGISTER_README));//$this->config_model->_get_by_specified_key('name', SOCIETY_REGISTER_README, 'value', TRUE)
		$data[] = array('description' => '社联成员注册说明', 'name' => 'ASSOCIATION_REGISTER_README', 'value' => str_replace("<br />", "\n", ASSOCIATION_REGISTER_README));
		
		$temp['data'] = $data;
		$this->load->view('admin/site_setting/main', $temp);
	}
	
	function site_setting_submit() {
		$constants_file = read_file(CONSTANTS_CONFIG_FILE);
		
		$site_setting_array = $this->input->post("site_setting_array");
		
		$site_name_full = "/define\('SITE_NAME_FULL', '(.*?)'\);/";
		$site_name_eng = "/define\('SITE_NAME_ENG', '(.*?)'\);/";
		$site_name_chs = "/define\('SITE_NAME_CHS', '(.*?)'\);/";
		$site_title_chs = "/define\('SITE_TITLE_CHS', '(.*?)'\);/";
		$society_register_chs = "/define\('SOCIETY_REGISTER_README', '(.*?)'\);/";
		$association_register_chs = "/define\('ASSOCIATION_REGISTER_README', '(.*?)'\);/";
		
		if ($site_setting_array[0] != SITE_NAME_FULL) {
			$constants_file = preg_replace($site_name_full, "define('SITE_NAME_FULL', '" . $site_setting_array[0] . "');", $constants_file);
		}
		if ($site_setting_array[1] != SITE_NAME_ENG) {
			$constants_file = preg_replace($site_name_eng, "define('SITE_NAME_ENG', '" . $site_setting_array[1] . "');", $constants_file);
		}
		if ($site_setting_array[2] != SITE_NAME_CHS) {
			$constants_file = preg_replace($site_name_chs, "define('SITE_NAME_CHS', '" . $site_setting_array[2] . "');", $constants_file);
		}
		if ($site_setting_array[3] != SITE_TITLE_CHS) {
			$constants_file = preg_replace($site_title_chs, "define('SITE_TITLE_CHS', '" . $site_setting_array[3] . "');", $constants_file);
		}
		if ($site_setting_array[4] != SOCIETY_REGISTER_README) {
			$constants_file = preg_replace($society_register_chs, "define('SOCIETY_REGISTER_README', '" . str_replace("\n", "<br />", $site_setting_array[4]) . "');", $constants_file);
		}
		if ($site_setting_array[5] != ASSOCIATION_REGISTER_README) {
			$constants_file = preg_replace($association_register_chs, "define('ASSOCIATION_REGISTER_README', '" . str_replace("\n", "<br />", $site_setting_array[5]) . "');", $constants_file);
		}
		
		/*$update_data['value'] = str_replace("\n","<br />",$site_setting_array[4]);
		$this->config_model->_update_by_specified_key('name' , SOCIETY_REGISTER_README, $update_data);
		$update_data['value'] = str_replace("\n","<br />",$site_setting_array[5]);
		$this->config_model->_update_by_specified_key('name' , ASSOCIATION_REGISTER_README, $update_data);*/
		
		if (!write_file(CONSTANTS_CONFIG_FILE, $constants_file)) {
			$this->output->set_output(json_encode("failed"));
		} else {
			$this->output->set_output(json_encode("succeeded"));
		}
		
		//echo "<pre>";
		//print_r($constants_file);
		//return ;
	}
}